package org.aspire.graphics.draw;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import org.aspire.core.sys.GameException;
import org.aspire.data.Emptyable;
import org.aspire.graphics.Color;
import org.aspire.graphics.DrawGraphics;
import org.aspire.graphics.Frame;

/******************************************************************************
 * A draw graphics implementation for Java Standard Edition Graphics2D class.
 * 
 * @author JPACarabuena
 *****************************************************************************/
public class JavaStandardGraphics implements DrawGraphics, Emptyable {

	// ========================================================================
	// Constants
	// ========================================================================

	// ========================================================================
	// Static Methods
	// ========================================================================

	// ========================================================================
	// Fields
	// ========================================================================
	
	/**************************************************************************
	 * The Java Standard AWT draw graphics.
	 *************************************************************************/
	private Graphics2D graphics;

	private Color color;
	
	// ========================================================================
	// Constructors
	// ========================================================================

	// ========================================================================
	// Getters & Setters
	// ========================================================================

	/**************************************************************************
	 * Sets the Graphics2D for this Java Standard draw graphics.
	 * 
	 * @param g	the AWT Graphics2D object.
	 *************************************************************************/
	public void setGraphics2D(Graphics2D g) {
		this.graphics = g;
	}
	
	// ========================================================================
	// Override Methods
	// ========================================================================

	@Override
	public Color getColor() {
		return this.color;
	}
	
	@Override
	public void setColor(Color color) {
		this.color = color;
	}
	
	@Override
	public void drawLine(int x1, int y1, int x2, int y2) {
		this.graphics.setColor(new java.awt.Color(
				this.color.getRedComponent(),
				this.color.getGreenComponent(),
				this.color.getBlueComponent(),
				this.color.getAlpha()
				));
		this.graphics.drawLine(x1, y1, x2, y2);
	}

	@Override
	public void drawRect(int x, int y, int width, int height) {
		this.graphics.setColor(new java.awt.Color(
				this.color.getRedComponent(),
				this.color.getGreenComponent(),
				this.color.getBlueComponent(),
				this.color.getAlpha()
				));
		this.graphics.drawRect(x, y, width, height);
	}

	@Override
	public void fillRect(int x, int y, int width, int height) {
		this.graphics.setColor(new java.awt.Color(
				this.color.getRedComponent(),
				this.color.getGreenComponent(),
				this.color.getBlueComponent(),
				this.color.getAlpha()
				));
		this.graphics.fillRect(x, y, width, height);
	}

	@Override
	public void drawOval(int x, int y, int width, int height) {
		this.graphics.setColor(new java.awt.Color(
				this.color.getRedComponent(),
				this.color.getGreenComponent(),
				this.color.getBlueComponent(),
				this.color.getAlpha()
				));
		this.graphics.drawOval(x, y, width, height);
	}

	@Override
	public void fillOval(int x, int y, int width, int height) {
		this.graphics.setColor(new java.awt.Color(
				this.color.getRedComponent(),
				this.color.getGreenComponent(),
				this.color.getBlueComponent(),
				this.color.getAlpha()
				));
		this.graphics.fillOval(x, y, width, height);
	}

	@Override
	public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
			throws GameException {
		if(nPoints != xPoints.length || nPoints != yPoints.length) {
			throw new GameException("No. of Points Does Not Match");
		}

		this.graphics.setColor(new java.awt.Color(
				this.color.getRedComponent(),
				this.color.getGreenComponent(),
				this.color.getBlueComponent(),
				this.color.getAlpha()
				));
		this.graphics.drawPolygon(xPoints, yPoints, nPoints);
	}

	@Override
	public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints)
			throws GameException {
		if(nPoints != xPoints.length || nPoints != yPoints.length) {
			throw new GameException("No. of Points Does Not Match");
		}

		this.graphics.setColor(new java.awt.Color(
				this.color.getRedComponent(),
				this.color.getGreenComponent(),
				this.color.getBlueComponent(),
				this.color.getAlpha()
				));
		this.graphics.fillPolygon(xPoints, yPoints, nPoints);
	}

	@Override
	public void drawString(String str, int x, int y) {
		this.graphics.setColor(new java.awt.Color(
				this.color.getRedComponent(),
				this.color.getGreenComponent(),
				this.color.getBlueComponent(),
				this.color.getAlpha()
				));
		this.graphics.drawString(str, x, y);
	}

	@Override
	public void drawImage(Frame image, int x, int y) {
		this.graphics.drawImage((BufferedImage) image.getImage(), x, y, null);
	}

	@Override
	public void clear() {
		this.graphics.dispose();
		this.graphics = null;
	}

	@Override
	public boolean isEmpty() {
		return (this.graphics == null);
	}

	// ========================================================================
	// Methods
	// ========================================================================

	// ========================================================================
	// Inner & Anonymous Classes
	// ========================================================================
}